#include "fdt_net.h"
#include "fdt_daemon.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

static struct fdt_daemon dm;

static int tcp_msg_handler(struct fdt_socket* sock)
{
    char buf[256] = {0};
    int len = 0;

    fdt_sock_send_msg(sock, buf, len);
     // processing...
}

static struct fdt_sock_cb tcp_cb = {
    .func = tcp_msg_handler
};

static int tcp_srv_handler(struct fdt_socket* sock)
{
    struct fdt_socket* tcpsock = fdt_sock_accept(sock, NULL, NULL);

    fdt_daemon_add_socket(&dm, tcpsock, &tcp_cb);

    return 0;
}

static struct fdt_sock_cb tcp_srv_cb = {
    .func = tcp_srv_handler
};

int main()
{
    struct fdt_socket tcpsrvsock;
    struct sockaddr_in addr;
    
    fdt_daemon_init(&dm);

    make_tcp_srv_socket(&tcpsrvsock);

    fdt_sock_open(&tcpsrvsock, O_NONBLOCK);

    memset(&addr, 0, sizeof(addr)); 
    addr.sin_family = AF_INET;
    addr.sin_port = htons(50001);
    addr.sin_addr.s_addr = inet_addr("192.168.1.45"); 
    
    fdt_sock_bind(&tcpsrvsock, (struct sockaddr_in*)&addr, sizeof(struct sockaddr_in));

    fdt_sock_listen(&tcpsrvsock);   

    /*
     * add fdt socket to daemon and start the daemon
     */
    fdt_daemon_add_socket(&dm, &tcpsrvsock, &tcp_srv_cb);

    fdt_daemon_start(&dm);

    return 0;
}
